package cn.echo.ecc.service.impl;

import cn.echo.ecc.domain.Login;
import cn.echo.ecc.dto.LoginDto;
import cn.echo.ecc.dto.RefreshDto;
import cn.echo.ecc.enums.GlobalExceptionCode;
import cn.echo.ecc.mapper.LoginMapper;
import cn.echo.ecc.result.JSONResult;
import cn.echo.ecc.service.ILoginService;
import cn.echo.ecc.util.AssertUtil;
import cn.echo.ecc.util.HttpUtil;
import cn.echo.ecc.vo.LoginVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 登录表 服务实现类
 * </p>
 *
 * @author hulan
 * @since 2022-09-23
 */
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login> implements ILoginService {

    private static final String LOGIN_URL = "http://localhost:10030/oauth/token?client_id=%s&client_secret=%s&grant_type=password&username=%s&redirect_uri=http://www.baidu.com&password=%s";
    private static final String REFRESH_URL = "http://localhost:10030/oauth/token?grant_type=refresh_token&refresh_token=%s&client_id=%s&client_secret=%s";

    /**
     * 用户登录
     * @param loginDto
     * @return
     */
    @Override
    public JSONResult common(LoginDto loginDto) {
        String username = loginDto.getUsername();
        String password = loginDto.getPassword();
        Integer type = loginDto.getType();
        // 1.使用JSR303进行参数校验
        // 2.判断时前台用户还是后台用户，将client_id和client_secret写死
        String clientId = "";
        String secret = "";
        if (type == 0) {
            clientId = "admin";
            secret = "123";
        } else if (type == 1) {
            clientId = "website";
            secret = "123";
        }
        AssertUtil.isNotEmpty(clientId, GlobalExceptionCode.CLIENT_ERROR);
        // 3.替换登录地址的占位符
        String loginUrl = String.format(LOGIN_URL, clientId, secret, username, password);
        // 4.使用HttpUtil工具类发送请求
        String result = HttpUtil.sendPost(loginUrl, null);
        if (result.contains("error_description")) {
            JSONObject jsonObject = JSON.parseObject(result, JSONObject.class);
            return JSONResult.error(jsonObject.getString("error_description"));
        }
        // 5.创建loginVo对象返回给前端，设置过期时间
        LoginVo loginVo = JSON.parseObject(result, LoginVo.class);
        loginVo.setExpiresTime(loginVo.getExpires_in() + System.currentTimeMillis());
        return JSONResult.success(loginVo);
    }

    @Override
    public JSONResult refresh(RefreshDto refreshDto) {
        String refreshToken = refreshDto.getRefreshToken();
        Integer type = refreshDto.getType();
        // 1.使用JSR303进行参数校验
        // 2.判断时前台用户还是后台用户，将client_id和client_secret写死
        String clientId = "";
        String secret = "";
        if (type == 0) {
            clientId = "admin";
            secret = "123";
        } else if (type == 1) {
            clientId = "website";
            secret = "123";
        }
        AssertUtil.isNotEmpty(clientId, GlobalExceptionCode.CLIENT_ERROR);
        // 3.替换登录地址的占位符
        String refreshUrl = String.format(REFRESH_URL, refreshToken, clientId, secret);
        // 4.使用HttpUtil工具类发送请求
        String result = HttpUtil.sendPost(refreshUrl, null);
        if (result.contains("error_description")) {
            JSONObject jsonObject = JSON.parseObject(result, JSONObject.class);
            return JSONResult.error(jsonObject.getString("error_description"));
        }
        // 5.创建loginVo对象返回给前端，设置过期时间
        LoginVo loginVo = JSON.parseObject(result, LoginVo.class);
        loginVo.setExpiresTime(loginVo.getExpires_in() + System.currentTimeMillis());
        return JSONResult.success(loginVo);
    }
}
